﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;

namespace MaxLeaf.DAL.Web
{
  
    public class mary_icprcplyentryDAL : BaseDAL
    {
        public mary_icprcplyentryDAL()
            : base()
        {
            strTableName = "mary_icprcplyentry";
            strKeyFieldName = "FEntryID";
            strFieldNames = "FInterID,FItemID,FRelatedID,FUnitID,FBegQty,FEndQty,FUnitName,FPrice,FBegDate,FEndDate";
        }
        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FInterID", StringComparison.InvariantCultureIgnoreCase))
            {

                string strFUnitGroupID = GetFInterID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));
                if (strFUnitGroupID.Length == 0)
                {
                    drDestData[strFieldName] = DBNull.Value;
                }
                else
                {
                    drDestData[strFieldName] = strFUnitGroupID;


                }

            }
            else if (strFieldName.Equals("FRelatedID", StringComparison.InvariantCultureIgnoreCase))
            {


                drDestData[strFieldName] = new mary_t_SubMessageDAL().GetFInterID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));


            }
            else if (strFieldName.Equals("FUnitName", StringComparison.InvariantCultureIgnoreCase))
            {


                drDestData[strFieldName] = GetFUnitName(dbOrgCon, CvtFunc.ToString(drOrgData["FUnitID"]));


            }
            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetFUnitName(DBAccess dbOrgCon, string strFUnitID)
        {
            DataTable dtData = dbOrgCon.GetTableData("t_Measureunit", new Dictionary<string, object> { { "FItemID", strFUnitID } }, "FName");

            if (dtData != null && dtData.Rows.Count > 0)
            {
                return CvtFunc.ToString(dtData.Rows[0]["FName"]);
            }
            return string.Empty;
        }

        private string GetFInterID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFInterID)
        {
            mary_icprcplyDAL dal = new mary_icprcplyDAL();


            return dal.Get_FInterID(dbOrgCon, dbDestCon, strOrgFInterID);


        }

        protected override DataTable DAL_GetData(DBAccess dbOrgCon, DBAccess destCon, int intdataType, object objParam)
        {
            GetDataType dataType = (GetDataType)intdataType;

            if (dataType == GetDataType.OrgData)
            {
                return Getmary_icprcplyentry(dbOrgCon, CvtFunc.ToString(objParam));
            }


            return base.DAL_GetData(dbOrgCon, destCon, intdataType, objParam);
        }



        private DataTable Getmary_icprcplyentry(DBAccess dbOrgCon, string strFEntryID)
        {
            DataTable dtmary_icprcplyentry = null;
            if (dtmary_icprcplyentry == null)
            {
                dtmary_icprcplyentry = dbOrgCon.GetTableData("icprcplyentry", new Dictionary<string, object> { { "FEntryID", strFEntryID } });

            }
            return dtmary_icprcplyentry;
        }
    }
}